package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功！");
    }

    /**
     * 分页查询订单(服务端)
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,Long number,String beginTime,String endTime){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(number!=null,Orders::getNumber,number).between(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime,endTime);
        orderService.page(pageInfo,qw);
        return R.success(pageInfo);
    }

    /**
     * 更改派送状态(服务端)
     * @param orders
     * @return
     */
    @PutMapping

    public R deliveryStatus(@RequestBody Orders orders) {
        LambdaUpdateWrapper<Orders> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.set(Orders::getStatus, orders.getStatus()).eq(Orders::getId, orders.getId());
        boolean update = orderService.update(queryWrapper);
        return R.success(update);
    }

    /**
     * 分页查询订单(客户端)
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")

    public R userPageQuery(Integer page, Integer pageSize) {
        //select * from orders where user_id = ?
        //where user_id = ?
        log.info("页码为:{}", page, pageSize);
        //通过threadLocal获得用户id
        Long userId = BaseContext.getCurrentId();

        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, userId);
        orderService.page(pageInfo, queryWrapper);

        long total = pageInfo.getTotal();
        List<Orders> records = pageInfo.getRecords();

        List<OrdersDto> ordersDtos = records.stream().map((record) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record, ordersDto);
            Long recordId = record.getId();
            LambdaQueryWrapper<OrderDetail> orderDetailQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailQueryWrapper.eq(OrderDetail::getOrderId, recordId);
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailQueryWrapper);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());

        Map hashMap = new HashMap();
        hashMap.put("total", total);
        hashMap.put("records", ordersDtos);
        return R.success(hashMap);
    }
}
